package com.yyfax.ph.personal.authority.jpush;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.ph.base.YJPH;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * P层：绑定\解绑 极光推送
 *
 * @author ying_he
 * @date 2017/3/8 12:41
 */
public class JPushPresenter extends Presenter<JPushModel, JPushPresenter.View> {
	private static final String TAG = JPushPresenter.class.getSimpleName();
	// 请求参数类型type：绑定
	private static final String J_PUSH_TYPE_BIND = "0";
	// 请求参数类型type：解绑
	private static final String J_PUSH_TYPE_UNBIND = "1";//解绑此参数暂时不用，已经集成到忘记密码、用户注销接口中(直接传jpush_id)

	public JPushPresenter.View mView;

	public static class Generator implements PresenterFactory<JPushPresenter> {
		@Override
		public JPushPresenter create() {
			return new JPushPresenter();
		}
	}

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new JPushModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void jPushSuccess();

		void jPushFailure(String tips);
	}

	public void bindJPush() {
		execute(generateJPushParams(J_PUSH_TYPE_BIND));
	}

	public void unbindJPush() {
		execute(generateJPushParams(J_PUSH_TYPE_UNBIND));
	}

	private JPushModel.JPushParams generateJPushParams(String type) {
		JPushModel.JPushParams jPushParams = new JPushModel.JPushParams();
		jPushParams.regId = YJPH.getInstance().getPreferenceManager().getJPushRegId();
		CLog.i(TAG, "极光ID>>>" + jPushParams.regId + "绑定类型>>>" + type);
		jPushParams.type = type;

		return jPushParams;
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void JPushResult(JPushModel.JPushResult result) {
		CLog.i(TAG, "极光操作结果>>>" + result.toString());
		if (isResponseSuccess(result)) {
			mView.jPushSuccess();
		} else {
			mView.jPushFailure(result.getMsg());
		}
	}
}
